﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;
using PPAP.Model;

namespace PPAP.Web.Substitutes
{
    public partial class SetForCurrentUser : PageBase
    {
        ContractPersonBO cbo = new ContractPersonBO();
        UsersBO ubo = new UsersBO();
        SubstitutesBO sbo = new SubstitutesBO();
        LogBO lbo = new LogBO();
        DepartmentBO dbo = new DepartmentBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (ubo.IsAdmin(CurrentUser.Id))
                {
                    tr1.Visible = true;
                    tr2.Visible = true;
                    DataSet dlist = dbo.GetList("");
                    foreach (DataRow dr in dlist.Tables[0].Rows)
                    {
                        ddlDepartment.Items.Add(new ListItem(dr["DEPARTMENT"].ToString(), dr["ID"].ToString()));
                    }
                    ddlDepartment_SelectedIndexChanged(null, null);
                    DataSet ds2 = sbo.GetList(" ");
                    ds2.Tables[0].Columns.Add("ti");
                    foreach (DataRow dr in ds2.Tables[0].Rows)
                    {
                        if (DateTime.Parse(dr["Substitutesdate"].ToString()) > DateTime.Parse("1901-01-01"))
                            dr["ti"] = DateTime.Parse(dr["Substitutesdate"].ToString()).ToString("yyyy-MM-dd");
                    }
                    dgList.DataSource = ds2;
                    dgList.DataBind();
                }
                else
                {
                    DataSet ds = null;
                    if (CurrentUser.UnitId == "0")
                    {
                        ds = cbo.GetList(" supplierid=" + CurrentUser.UnitName + "  and email <>'" + CurrentUser.Id + "'");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            ddlSubstitutes.Items.Add(new ListItem("no substitute available", "no substitute available"));
                            btnSave.Enabled = false;
                        }
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            ddlSubstitutes.Items.Add(new ListItem(dr["email"].ToString(), dr["email"].ToString()));
                        }
                    }
                    else
                    {
                        ds = ubo.GetList(" username<>'admin' and username<>'" + CurrentUser.Id + "' and  u.departmentid=" + CurrentUser.UnitId);
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            ddlSubstitutes.Items.Add(new ListItem("no substitute available", "no substitute available"));
                            btnSave.Enabled = false;
                        }
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            ddlSubstitutes.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
                        }
                    }
                    DataSet ds2 = sbo.GetList(" username='" + CurrentUser.Id + "'");
                    ds2.Tables[0].Columns.Add("ti");
                    foreach (DataRow dr in ds2.Tables[0].Rows)
                    {
                        if (DateTime.Parse(dr["Substitutesdate"].ToString()) > DateTime.Parse("1901-01-01"))
                            dr["ti"] = DateTime.Parse(dr["Substitutesdate"].ToString()).ToString("yyyy-MM-dd");
                    }
                    dgList.DataSource = ds2;
                    dgList.DataBind();
                }
            }
            if (Request.QueryString["SID"] != null)
            {
                DeleteSu(Request.QueryString["SID"].ToString());
            }
        }

        public void DeleteSu(string SID)
        {
            int num = sbo.Delete(int.Parse(SID));
            if(num>0)
                AlertRedirect("Delete success", "SetForCurrentUser.aspx");
        }

        protected override void SkinControl()
        {

        }

        protected void cbEnable_CheckedChanged(object sender, EventArgs e)
        {
            txtSubstitutesDate.Visible = cbEnable.Checked;
            Label6.Visible = cbEnable.Checked;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ubo.IsAdmin(CurrentUser.Id))
            {
                Model.Substitutes su = new Model.Substitutes();
                su.Substitutesdate = txtSubstitutesDate.Text.Trim() == "" ? DateTime.Parse("1900-01-01") : DateTime.Parse(txtSubstitutesDate.Text);
                su.SUBSTITUTESENABLE = 1;// cbEnable.Checked ? 1 : 0;
                su.SUBSTITUTESUSER = ddlSubstitutes.SelectedValue;
                su.USERNAME = ddlUser.SelectedValue;
                DataSet ds = sbo.GetList(" username='" + su.USERNAME + "' and substitutesuser='" + su.SUBSTITUTESUSER + "' ");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    AlertMessage("Record already exist, please first delete this record");
                    return;
                }
                int num = sbo.Add(su);
                lbo.WriteLog(CurrentUser.Id, "Substitutes(" + su.SUBSTITUTESUSER + ")");
                if (num > 0)
                    AlertReLoad("Save success");
            }
            else
            {
                Model.Substitutes su = new Model.Substitutes();
                su.Substitutesdate = txtSubstitutesDate.Text.Trim() == "" ? DateTime.Parse("1900-01-01") : DateTime.Parse(txtSubstitutesDate.Text);
                su.SUBSTITUTESENABLE = 1;// cbEnable.Checked ? 1 : 0;
                su.SUBSTITUTESUSER = ddlSubstitutes.SelectedValue;
                su.USERNAME = CurrentUser.Id;
                DataSet ds = sbo.GetList(" username='" + su.USERNAME + "' and substitutesuser='" + su.SUBSTITUTESUSER + "' ");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    AlertMessage("Record already exist, please first delete this record");
                    return;
                }
                int num = sbo.Add(su);
                lbo.WriteLog(CurrentUser.Id, "Substitutes(" + su.SUBSTITUTESUSER + ")");
                if (num > 0)
                    AlertReLoad("Save success");
            }
        }

        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlUser.Items.Clear();
            ddlSubstitutes.Items.Clear();
            DataSet ds= ubo.GetList(" username<>'admin' and username<>'" + CurrentUser.Id + "' and  u.departmentid=" + ddlDepartment.SelectedValue);
            if (ds.Tables[0].Rows.Count == 0)
            {
                ddlSubstitutes.Items.Add(new ListItem("no substitute available", "no substitute available"));
                ddlUser.Items.Add(new ListItem("no substitute available", "no substitute available"));
                //btnSave.Enabled = false;
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ddlSubstitutes.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
                ddlUser.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
            }
        }
    }
}